﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.ServiceModel.Configuration;
using BizTalkOneService;

namespace BizTalkOneClient
{
    public class BizTalkCounters
    {

        List<BizTalkGroup> BizTalkGroupList = new List<BizTalkGroup>();

        public BizTalkCounters()
        {
            ClientSection clientSection = (ClientSection)WebConfigurationManager.GetSection("system.serviceModel/client");

            for (int i = 0; i < clientSection.Endpoints.Count; i++)
            {
                ChannelEndpointElement endpoint = clientSection.Endpoints[i];
                try
                {
                    BizTalkGroupList.Add(new BizTalkGroup { BizTalkGroupCounterList = new BizTalkOneClient.BizTalkOneServiceReference.BizTalkOneServiceClient(endpoint.Name).GetCounterList(), Name = endpoint.Name});
                }
                catch (Exception ex)
                {
                    BizTalkOneService.Counter[] counter = new BizTalkOneService.Counter[1];
                    counter[0] = new Counter() { CounterCategory = "Error", CounterName = endpoint.Name, CounterValueString = String.Format("{0}", ex.Message) };
                    BizTalkGroupList.Add(new BizTalkGroup() { BizTalkGroupCounterList = counter, Name = endpoint.Name   });
                }
            }

        }

        public List<string> GetGroupNames()
        {
            List<string> result = new List<string>();
            foreach (var item in BizTalkGroupList)
            {
                result.Add(item.Name);
            }
            return result;
        }


        public List<string> GetCategoryList(string groupname)
        {
            List<string> result = new List<string>();
            foreach (var item in BizTalkGroupList)
            {
                if (item.Name == groupname)
                {
                    foreach (var counter in item.BizTalkGroupCounterList)
                    {
                        result.Add(counter.CounterCategory.ToString() );
                    }
                }
            }
            return result.Distinct().ToList();
        }

        public List<Counter> GetCounterByCategory(string category, string groupname) 
        {
            List<Counter> counterList = new List<Counter>();
            foreach (var item in BizTalkGroupList)
            {
                if (item.Name == groupname)
                {
                    foreach (var counter in item.BizTalkGroupCounterList)
                    {
                        if (counter.CounterCategory == category)
                        {
                            counterList.Add(new Counter { CounterCategory = counter.CounterCategory, CounterName = counter.CounterName, CounterValue = counter.CounterValue, CounterValueString = counter.CounterValueString, ServerName = counter.ServerName });
                        }
                    }
                }
            }

            return counterList;
        }

    }
}